﻿using System.Collections.Concurrent;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.Core.Infrastructure.Base;
using ZilLion.Service.TaskPlatform.Controllers.StateContainer;
using ZilLion.Service.TaskPlatform.Controllers.Websocket;
using ZilLion.Service.TaskPlatform.Domain;
using ZilLion.Service.TaskPlatform.Domain.Entity;
using ZilLion.Service.TaskPlatform.Domain.Response;
using ZilLion.Service.TaskPlatform.Domain.Respository.Interface;
using ZilLion.Service.TaskPlatform.Domain.TaskCommand;

namespace ZilLion.Service.TaskPlatform.Controllers
{
    /// <summary>
    ///     任务命令
    /// </summary>
    [Route("Api/TaskPlatform/TaskCommand")]
    [AllowAnonymous]
    public class TaskCommandController : ZilLionControllerBase
    {
        private readonly ITaskCommandRespository _taskCommandRespository;
        private readonly TaskPlatformWebSocketHandler _taskPlatformWebSocketHandler;
        private readonly TaskRunStateContainer _taskRunStateContainer;


        public TaskCommandController(ITaskCommandRespository taskCommandRespository,
            TaskPlatformWebSocketHandler taskPlatformWebSocketHandler, TaskRunStateContainer taskRunStateContainer)
        {
            _taskCommandRespository = taskCommandRespository;
            _taskPlatformWebSocketHandler = taskPlatformWebSocketHandler;
            _taskRunStateContainer = taskRunStateContainer;
        }

        /// <summary>
        ///     获取全部command
        /// </summary>
        /// <param name="nodeid"></param>
        /// <returns></returns>
        [HttpGet("AllTaskConmmand")]
        public TaskCommandResponse GetAllTaskConmmand(string nodeid = null)
        {
            var entitys = _taskCommandRespository.GetAllTaskConmmandEntities(nodeid);
            return new TaskCommandResponse {TaskConmmands = entitys};
        }

        /// <summary>
        ///     获取全部未处理的节点
        /// </summary>
        /// <param name="nodeid"></param>
        /// <returns></returns>
        [HttpGet("UnHandleConmmand")]
        public TaskCommandResponse GetUnHandleTaskConmmandEntities(string nodeid)
        {
            var entitys = _taskCommandRespository.GetUnHandleTaskConmmandEntitiesAsync(nodeid);
            return new TaskCommandResponse {TaskConmmands = entitys};
        }

        /// <summary>
        ///     保存command
        /// </summary>
        /// <param name="command"></param>
        [HttpPost("SaveTaskConmmand")]
        public ResponseBase SaveTaskConmmandEntity([FromBody] TaskConmmandEntity command)
        {
            if (command != null)
                _taskCommandRespository.SaveTaskConmmandEntity(command);

            return new ResponseBase();
        }


        [HttpPost("StartTask")]
        public ResponseBase StartTask([FromBody] TaskStateEntity statedto)
        {
            if (statedto != null)
            {
                ConcurrentDictionary<string, TaskStateEntity> statesdic = null;
                if (_taskRunStateContainer.TaskStates.TryGetValue(statedto.NodeId, out statesdic))
                    if (statesdic != null)
                        if (statesdic.TryGetValue(statedto.Taskid, out var state))
                            if (state != null)
                            {
                                state.TaskErrorCount = 0;
                                statesdic.AddOrUpdate(state.Taskid, state);
                            }
                TaskCommandHelper.CreateTaskCommand(statedto.NodeId, statedto.Taskid,
                    EnumTaskCommandName.StartTask, _taskPlatformWebSocketHandler);
            }

            return new ResponseBase();
        }

        [HttpPost("StopTask")]
        public ResponseBase StopTask([FromBody] TaskStateEntity statedto)
        {
            if (statedto != null)
                TaskCommandHelper.CreateTaskCommand(statedto.NodeId, statedto.Taskid,
                    EnumTaskCommandName.StopTask, _taskPlatformWebSocketHandler);
            return new ResponseBase();
        }
    }
}